#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
void solve()
{
    map<string, string> mp;
    map<string, bool> mb;
    ll n, m;
    cin >> n >> m;
    vector<string> s(n + 1);
    for (ll i = 1; i <= n; i++)
    {
        string q;
        cin >> s[i] >> q;
        mp[s[i]] = q;
        mb[s[i]] = true;
    }
    while (m--)
    {
        string ss;
        cin >> ss;

        if (mb[ss])
        {
            cout << mp[ss] << endl;
            continue;
            ;
        }
        ll cnt = 0;
        string ans;
        for (ll i = 0; i < ss.size(); i++)
        {
            string s1 = ss.substr(0, i + 1), s2 = ss.substr(i + 1);
            if (mb[s1] && mb[s2])
            {
                ans += mp[s1] + mp[s2];
                cnt++;
            }
        }
        if (cnt == 1)
        {
            cout << ans << endl;
        }
        else
        {
            cout << "D" << endl;
        }
    }
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
